#include <pspkernel.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <pspaudio.h>
#include <pspaudiolib.h>
#include <psppower.h>
#include <pspdisplay.h>

#include "mp3player.h"

PSP_MODULE_INFO("Mp3 Player Example", 0, 1, 1);
#define printf pspDebugScreenPrintf 
PSP_HEAP_SIZE_KB(20480);

// TWILIGHT ZONE! <do doo do doo>
/* Exit callback */
int exit_callback(int arg1, int arg2, void *common) {
          sceKernelExitGame();
          return 0;
}

/* Callback thread */
int CallbackThread(SceSize args, void *argp) {
          int cbid;

          cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
          sceKernelRegisterExitCallback(cbid);

          sceKernelSleepThreadCB();

          return 0;
}

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void) {
          int thid = 0;

          thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
          if(thid >= 0) {
                    sceKernelStartThread(thid, 0, 0);
          }

          return thid;
}

int main() 
{
		scePowerSetClockFrequency(333, 333, 166);

		
		
		pspDebugScreenInit();
		SetupCallbacks();

		pspAudioInit();
		SceCtrlData pad;
		int i; 

		
		KMp3Stream stream("test.mp3");
		KAudioChannel myChannel(1);
		myChannel.PlayStream(stream);
		
		
		while(1) {
			sceCtrlReadBufferPositive(&pad, 1);
			if(pad.Buttons & PSP_CTRL_CROSS) {
					myChannel.PlayStream(stream);
					
			} else if(pad.Buttons & PSP_CTRL_CIRCLE) {
					  //pPlay->MP3_Pause();
					  for(i=0; i<10; i++) {
								sceDisplayWaitVblankStart();
					  }
			}

			/*if (pPlay->MP3_EndOfStream() == 1) {
					  pPlay->MP3_Stop();
			}*/
			myChannel.TimeElapse(0);
		} 
	//pPlay->MP3_Stop();
    //pPlay->MP3_FreeTune();

    sceKernelSleepThread();

    return 0;
}
